#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_freertos_hooks.h"
#include "freertos/semphr.h"
#include "esp_system.h"
#include "driver/gpio.h"
#include"key.h"
void key_init()
{
    gpio_reset_pin(BLINK_GPIO);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);


    //zero-initialize the config structure.
    gpio_config_t io_conf = {};
    //disable interrupt
    io_conf.intr_type = GPIO_INTR_DISABLE;
    //set as output mode
    io_conf.mode = GPIO_MODE_OUTPUT;
    //bit mask of the pins that you want to set,e.g.GPIO18/19
    io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
    //disable pull-down mode
    io_conf.pull_down_en = 0;
    //disable pull-up mode
    io_conf.pull_up_en = 0;
    //configure GPIO with the given settings
    gpio_config(&io_conf);

    //interrupt of rising edge
    io_conf.intr_type = GPIO_INTR_POSEDGE;
    //bit mask of the pins, use GPIO4/5 here
    io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
    //set as input mode
    io_conf.mode = GPIO_MODE_INPUT;
    //enable pull-up mode
    io_conf.pull_up_en = 1;
    gpio_config(&io_conf);
    
}
uint8_t KEY_Scan(uint8_t mode)
{	 
   
	static uint8_t key_up=1;   /*按键按松开标志，key_up=1为无按键按下，key_up=0为有按键按下*/
	static uint8_t KEY0;
    KEY0 =gpio_get_level(center_key);
    printf("KEY0 aaa= %d\n",KEY0);
    static uint8_t KEY1;
    static uint8_t KEY2;
    static uint8_t WK_UP;
	if(mode)key_up=1;    //支持连按		
	if(key_up && (KEY0==0||KEY1==0||KEY2==0||WK_UP==1))     /*任意一个按键按下*/
	{
		vTaskDelay(pdMS_TO_TICKS(10));   //去抖动 
		key_up=0;        //有按键按下
		if(KEY0==0)return 1;             
		else if(KEY1==0)return 2;
		else if(KEY2==0)return 3;
		else if(WK_UP==1)return 4;
	}
	else if(KEY0==1&&KEY1==1&&KEY2==1&&WK_UP==0)key_up=1;  /*还是默认电平，说明无按键按下*/	    
 	return 0;
}
